// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.NavAfterSellOrder;
import com.fowo.api.entity.NavSellOrder;
import com.fowo.api.model.nav.after.sell.order.CustomNavAfterSellOrderRecordRemarkActionVo;
import com.fowo.api.model.nav.after.sell.order.NavAfterSellOrderVo;
import com.fowo.api.model.nav.after.sell.order.detail.NavAfterSellOrderDetailItemVo;
import com.fowo.api.model.nav.after.sell.order.detail.NavAfterSellOrderDetailSearchParamPo;
import com.fowo.api.model.nav.sell.order.NavSellOrderVo;
import com.fowo.api.model.nav.sell.order.declare.NavSellOrderDeclareItemVo;
import com.fowo.api.model.nav.sell.order.declare.NavSellOrderDeclareSearchParamPo;
import com.fowo.api.model.nav.sell.order.detail.NavSellOrderDetailItemVo;
import com.fowo.api.model.nav.sell.order.detail.NavSellOrderDetailSearchParamPo;
import com.fowo.api.model.nav.sell.order.platform.NavSellOrderPlatformItemVo;
import com.fowo.api.model.nav.sell.order.platform.NavSellOrderPlatformSearchParamPo;
import com.fowo.api.model.nav.sell.order.transaction.NavSellOrderTransactionItemVo;
import com.fowo.api.model.nav.sell.order.transaction.NavSellOrderTransactionSearchParamPo;
import com.fowo.api.service.NavAfterSellOrderService;
import com.fowo.api.sys.annotation.DataActivity;
import com.fowo.api.sys.entity.SysBase;
import com.fowo.api.sys.entity.enums.SysDataActivityAction;
import com.fowo.api.user.model.JwtUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单管理售后工单 服务实现类
 */
@Service
@Slf4j
@DS("ds24")
public class NavAfterSellOrderServiceImpl
        extends NavAfterSellOrderBaseServiceImpl implements NavAfterSellOrderService {

    @Resource
    private NavSellOrderServiceImpl navSellOrderService;

    /**
     * 获取列表详情
     *
     * @param idList 订单管理的主键
     */
    public List<NavAfterSellOrderVo> getVoByIdList(List<Long> idList, boolean containsSub) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(idList), "参数异常!");
        List<NavAfterSellOrder> ls = this.listByIds(idList);
        if (CollUtil.isEmpty(ls)) {
            return null;
        }
        List<NavAfterSellOrderVo> list = BeanUtil.copyToList(ls, NavAfterSellOrderVo.class);
        if (containsSub) {
            List<Long> navSellOrderIdList = list.stream().map(m -> m.getNavSellOrderId()).collect(Collectors.toList());
            NavAfterSellOrderDetailSearchParamPo detailSearchParamPo =
                    new NavAfterSellOrderDetailSearchParamPo();
            detailSearchParamPo.setParentNavAfterSellOrderInList(idList);
            detailSearchParamPo.applySqlSegments();
            List<NavAfterSellOrderDetailItemVo> afterSellOrderDetailItemVoList = navAfterSellOrderDetailService.search(detailSearchParamPo);
            NavSellOrderPlatformSearchParamPo platformSearchParamPo =
                    new NavSellOrderPlatformSearchParamPo();
            platformSearchParamPo.setParentNavSellOrderInList(navSellOrderIdList);
            platformSearchParamPo.applySqlSegments();
            List<NavSellOrderPlatformItemVo> platformItemVoList = navSellOrderPlatformService.search(platformSearchParamPo);
            NavSellOrderTransactionSearchParamPo transactionSearchParamPo =
                    new NavSellOrderTransactionSearchParamPo();
            transactionSearchParamPo.setParentNavSellOrderInList(navSellOrderIdList);
            transactionSearchParamPo.applySqlSegments();
            List<NavSellOrderTransactionItemVo> transactionItemVoList = navSellOrderTransactionService.search(transactionSearchParamPo);
            NavSellOrderDetailSearchParamPo productSearchParamPo =
                    new NavSellOrderDetailSearchParamPo();
            productSearchParamPo.setParentNavSellOrderInList(navSellOrderIdList);
            productSearchParamPo.applySqlSegments();
            List<NavSellOrderDetailItemVo> detailItemVoList = navSellOrderDetailService.search(productSearchParamPo);
            NavSellOrderDeclareSearchParamPo declareSearchParamPo =
                    new NavSellOrderDeclareSearchParamPo();
            declareSearchParamPo.setParentNavSellOrderInList(navSellOrderIdList);
            declareSearchParamPo.applySqlSegments();
            List<NavSellOrderDeclareItemVo> declareItemVoList = navSellOrderDeclareService.search(declareSearchParamPo);
            for (NavAfterSellOrderVo item : list) {
                item.setDetail(CollUtil.isEmpty(afterSellOrderDetailItemVoList) ? null : afterSellOrderDetailItemVoList.stream().filter(f -> NumberUtil.equals(f.getParentNavAfterSellOrder(), item.getId())).collect(Collectors.toList()));
                item.setPlatform(CollUtil.isEmpty(platformItemVoList) ? null : platformItemVoList.stream().filter(f -> NumberUtil.equals(f.getParentNavSellOrder(), item.getNavSellOrderId())).collect(Collectors.toList()));
                item.setDeclare(CollUtil.isEmpty(declareItemVoList) ? null : declareItemVoList.stream().filter(f -> NumberUtil.equals(f.getParentNavSellOrder(), item.getNavSellOrderId())).collect(Collectors.toList()));
                item.setProduct(CollUtil.isEmpty(detailItemVoList) ? null : detailItemVoList.stream().filter(f -> NumberUtil.equals(f.getParentNavSellOrder(), item.getNavSellOrderId())).collect(Collectors.toList()));
                item.setTransaction(CollUtil.isEmpty(transactionItemVoList) ? null : transactionItemVoList.stream().filter(f -> NumberUtil.equals(f.getParentNavSellOrder(), item.getNavSellOrderId())).collect(Collectors.toList()));

            }
        }
        return list;
    }

    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了创建操作"
    )
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long create(NavAfterSellOrderVo model) throws Exception {
        model.setProduct(null);
        model.setPlatform(null);
        model.setTransaction(null);
        model.setDeclare(null);
        Assert.isTrue(CollUtil.size(model.getDetail()) > 0, "售后信息不能为空!");
        return super.create(model);
    }


    /**
     * 提交
     * 若选择售后类型为补发的售后单，则有补发信息模块，支持自动填充原订单相关信息，并且提交后（若需要审批，则需要审批通过），自动生成补发单；提交售后单即确定库存扣减
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了提交操作"
    )
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void submit(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        try {
            List<NavAfterSellOrderVo> list = this.getVoByIdList(ids, false);
            Assert.isTrue(CollUtil.isNotEmpty(list), "请选择对应订单!");
            Assert.isTrue(list.stream().allMatch(a -> CollUtil.safeContains(CollUtil.newArrayList("1"), a.getStatus())), "校验异常,请检查单据状态!");// 1:待提交 2:待审核 3:处理中 4:已完成 -1:已作废
//            for (NavAfterSellOrder navAfterSellOrder : list) {
//                //是否补发
//                if (StrUtil.equals("2", navAfterSellOrder.getType())) {//1:仅退款 2:补发
//                    if (StrUtil.equals("1", navSellOrderService.warehouseHandler(navAfterSellOrder.getDeliveryWarehouse()))) { //1 - 本地仓; 2 - 海外仓; 3 - FBA
//                        // 扣库存
//                        this.subInventory(navAfterSellOrder.getId());
//                    }
//                }
//            }
            //03 更新状态 1:待提交 2:待审核 3:处理中 4:已完成 -1:已作废
            this.changeNavAfterSellOrderStatus(list, "2", null);
        } catch (Exception ex) {
            log.error("发生异常,异常信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
            throw new RuntimeException("发生异常,信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
        }
    }


    /**
     * 审核
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了审核操作"
    )
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void process(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        try {
            //01 验证
            List<NavAfterSellOrderVo> list = this.getVoByIdList(ids, false);
            Assert.isTrue(CollUtil.isNotEmpty(list), "请选择对应订单!");
            Assert.isTrue(list.stream().allMatch(a -> CollUtil.safeContains(CollUtil.newArrayList("2"), a.getStatus())), "校验异常,请检查单据状态!");// 1:待提交 2:待审核 3:处理中 4:已完成 -1:已作废
            //02 更新状态 1:待提交 2:待审核 3:处理中 4:已完成 -1:已作废
            this.changeNavAfterSellOrderStatus(list, "3", null);
        } catch (Exception ex) {
            log.error("发生异常,异常信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
            throw new RuntimeException("发生异常,信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
        }
    }


    /**
     * 标记退款
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了标记退款操作"
    )
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void issuRefund(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        try {
            List<NavAfterSellOrderVo> list = this.getVoByIdList(ids, false);
            Assert.isTrue(CollUtil.isNotEmpty(list), "请选择对应订单!");
            Assert.isTrue(list.stream().allMatch(a -> CollUtil.safeContains(CollUtil.newArrayList("1"), a.getType())), "校验异常,包含非仅退款售后类型!");// 1:仅退款 2:补发
            Assert.isTrue(list.stream().allMatch(a -> CollUtil.safeContains(CollUtil.newArrayList("3"), a.getStatus())), "校验异常,请检查单据状态!");// 1:待提交 2:待审核 3:处理中 4:已完成 -1:已作废
            for (NavAfterSellOrderVo model : list) {
                model.setRefundAmount(NumberUtil.add(model.getRefundAmount(), model.getThisAmount()));
            }
            //03 更新状态 1:待提交 2:待审核 3:处理中 4:已完成 -1:已作废
            this.changeNavAfterSellOrderStatus(list, "4", "2"); //1: 待退款 2: 退款中 3: 退款失败 4: 退款完成
            //03 截单
            List<Long> cutList = list.stream().filter(f -> StrUtil.equals("1", f.getCut())).map(m -> m.getNavSellOrderId()).collect(Collectors.toList());
            if (CollUtil.isEmpty(cutList)) {
                return;
            }
            List<Long> navSellOrderIdList = navSellOrderService.listObjs(Wrappers.<NavSellOrder>lambdaQuery().in(NavSellOrder::getId, cutList).in(NavSellOrder::getStatus, CollUtil.newArrayList("3")).select(NavSellOrder::getId), m -> Convert.toLong(m));
            if (CollUtil.isEmpty(navSellOrderIdList)) {
                return;
            }
            navSellOrderService.cut(navSellOrderIdList);

        } catch (Exception ex) {
            log.error("发生异常,异常信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
            throw new RuntimeException("发生异常,信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
        }
    }

    /**
     * 标记补发
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了标记补发操作"
    )
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void issuReturn(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        try {
            List<NavAfterSellOrderVo> list = this.getVoByIdList(ids, true);
            Assert.isTrue(CollUtil.isNotEmpty(list), "请选择对应订单!");
            Assert.isTrue(list.stream().allMatch(a -> CollUtil.safeContains(CollUtil.newArrayList("3"), a.getStatus())), "校验异常,请检查单据状态!");// 1:待提交 2:待审核 3:处理中 4:已完成 -1:已作废
            Assert.isTrue(list.stream().allMatch(a -> CollUtil.safeContains(CollUtil.newArrayList("2"), a.getType())), "校验异常,包含非补发售后类型!");// 1:仅退款 2:补发
            //重复生成检测
            final List<NavSellOrder> repeatList = navSellOrderService.list(Wrappers.<NavSellOrder>lambdaQuery().in(NavSellOrder::getOriginalOrderId, list.stream().map(m -> m.getOrderId()).collect(Collectors.toList())));
            Assert.isTrue(CollUtil.isEmpty(repeatList), "重复生成订单,单号:{}", repeatList.stream().map(m -> m.getOrderId()).collect(Collectors.joining(",")));
            for (NavAfterSellOrderVo navAfterSellOrder : list) {
                //是否补发
                if (StrUtil.equals("2", navAfterSellOrder.getType())) { //1:仅退款 2:补发
                    NavSellOrderVo originalOrderEntity = navSellOrderService.getVoById(navAfterSellOrder.getNavSellOrderId());
                    Assert.isTrue(ObjUtil.isNotEmpty(originalOrderEntity), "关联订单不存在!");
                    NavSellOrderVo navSellOrderEntity = BeanUtil.copyProperties(originalOrderEntity, NavSellOrderVo.class, Arrays.stream(ReflectUtil.getFields(SysBase.class)).map(m -> ReflectUtil.getFieldName(m)).toArray(String[]::new));
                    navSellOrderEntity.setTag("28");
                    navSellOrderEntity.setStatus("1");
                    //订单标签 1:待人工审核 2:留言处理 3:地址错误 4:商品未配对 5:黑名单 6:未分配仓库 7:未分配物流 8:可合并订单 9:合并订单 10:物流下单失败 11:超时未发货 12:买家取消订单 13:其他 14:买家申请取消 15:仓库物流不匹配 16:第三方作废 17:第三方仓商品未配对 18:第三方仓未开启/第三方仓发货单暂未创建 19:审核失败 20:审核超时请重试 21:可拆分订单 22:拆分订单 23:转FBA配送 24:留言已处理 25:缺货
                    //26:冻结订单 27:风险订单 28:补发订单
                    navSellOrderEntity.setOriginalOrderId(navSellOrderEntity.getOrderId());
                    //不用复制
                    navSellOrderEntity.setDeliveryWaybillId(null);//物流商返回的物流单号
                    navSellOrderEntity.setDeliveryTrackId(null);//跟踪号
                    navSellOrderEntity.setDeliveryPartnerOrderId(null);
//                  navSellOrderEntity.setBuyerName(navAfterSellOrder.getBuyerName());//买家姓名
//                  navSellOrderEntity.setBuyerEmail(navAfterSellOrder.getBuyerEmail());//买家邮箱
                    navSellOrderEntity.setDeliveryReceipt(navAfterSellOrder.getDeliveryReceipt());//收件人
                    navSellOrderEntity.setDeliveryZipCode(navAfterSellOrder.getDeliveryZipCode());//邮编
                    navSellOrderEntity.setDeliveryPhone(navAfterSellOrder.getDeliveryPhone());//电话
                    navSellOrderEntity.setDeliveryCountry(navAfterSellOrder.getDeliveryCountry());//国家/地区
                    navSellOrderEntity.setDeliveryProvince(navAfterSellOrder.getDeliveryProvince());//省/州
                    navSellOrderEntity.setDeliveryCity(navAfterSellOrder.getDeliveryCity());//城市
                    navSellOrderEntity.setDeliveryArea(navAfterSellOrder.getDeliveryArea());//区/县
                    navSellOrderEntity.setDeliveryHouseNumber(navAfterSellOrder.getDeliveryHouseNumber());//门牌号
                    navSellOrderEntity.setDeliveryCompany(navAfterSellOrder.getDeliveryCompany());//公司名
                    navSellOrderEntity.setDeliveryAddressType(navAfterSellOrder.getDeliveryAddressType());//地址类型
                    navSellOrderEntity.setDeliveryAddress1(navAfterSellOrder.getDeliveryAddress1());//详细地址1
                    navSellOrderEntity.setDeliveryAddress2(navAfterSellOrder.getDeliveryAddress2());//详细地址2
                    navSellOrderEntity.setDeliveryAddress3(navAfterSellOrder.getDeliveryAddress3());//详细地址3
                    navSellOrderEntity.setDeliveryWarehouse(navAfterSellOrder.getDeliveryWarehouse());//发货仓库
                    navSellOrderEntity.setDeliveryLogisticsChannel(navAfterSellOrder.getDeliveryLogisticsChannel());//物流方式
                    navSellOrderEntity.setRemark(navAfterSellOrder.getRemark());//客服备注
                    //子表处理
                    List<NavSellOrderDetailItemVo> productList = new ArrayList<>();
                    for (NavAfterSellOrderDetailItemVo item : navAfterSellOrder.getDetail()) {
                        //找到原订单产品修改数量(交易信息?)
                        NavSellOrderDetailItemVo product = CollUtil.findOne(navSellOrderEntity.getProduct(), f -> NumberUtil.equals(item.getNavSellOrderDetailId(), f.getId()));
                        Assert.isTrue(ObjUtil.isAllNotEmpty(product), "售后信息产品不存在!");
                        product.setSalesRevenueAmount(BigDecimal.ZERO);//销售收益
                        product.setAvailableQuantity(0);//可用量
                        product.setTax(BigDecimal.ZERO);//客付税费
                        product.setTip(BigDecimal.ZERO);//小费
                        product.setDiscount(BigDecimal.ZERO);//折扣
                        product.setDeliveryThinkFreight(BigDecimal.ZERO);//预估运费
                        product.setDeliveryRealFreight(BigDecimal.ZERO);//客付运费
                        product.setTransactionCost(BigDecimal.ZERO);//交易费
                        product.setOtherAmount(BigDecimal.ZERO);//平台其他费
                        product.setWmsOutboundCostAmount(BigDecimal.ZERO);//实际出库成本
                        product.setCodAmount(BigDecimal.ZERO);//COD费用
                        product.setWmsShippingPriceAmount(BigDecimal.ZERO);//实际运费
                        product.setGiftWrapAmount(BigDecimal.ZERO);//礼品包装费
                        product.setSaleTax(BigDecimal.ZERO);//销售税
                        product.setPointsGrantedAmount(BigDecimal.ZERO);//积分成本
                        product.setOtherFee(BigDecimal.ZERO);//其他费用
                        //主要计算下面单价*数量=商品金额
                        product.setPrice(item.getPrice());
                        product.setQuantity(item.getAfterQuantity());
                        product.setAmount(ObjUtil.isEmpty(item.getAmount()) ? NumberUtil.mul(product.getPrice(), product.getQuantity()) : item.getAmount());
                        product.setRemark(item.getRemark());
                        CollUtil.addIfAbsent(productList, product);
                    }
                    navSellOrderEntity.setDeclare(null);
                    navSellOrderEntity.setTransaction(null);
                    navSellOrderEntity.setProduct(productList);
                    navSellOrderService.create(navSellOrderEntity);
                }
            }
            //03 更新状态 1:待提交 2:待审核 3:处理中 4:已完成 -1:已作废
            this.changeNavAfterSellOrderStatus(list, "4", null); //1: 待收货 2: 退货完成 3: 退货作废
        } catch (Exception ex) {
            log.error("发生异常,异常信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
            throw new RuntimeException("发生异常,信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
        }
    }


    /**
     * 作废
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了作废操作"
    )
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void cancel(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        try {
            List<NavAfterSellOrderVo> list = this.getVoByIdList(ids, false);
            Assert.isTrue(CollUtil.isNotEmpty(list), "请选择对应订单!");
            Assert.isTrue(list.stream().allMatch(a -> CollUtil.safeContains(CollUtil.newArrayList("1", "2", "3", "4"), a.getStatus())), "校验异常,请检查单据状态!");// 1:待提交 2:待审核 3:处理中 4:已完成 -1:已作废
//            for (NavAfterSellOrder navAfterSellOrder : list) {
//                //是否补发
//                if (StrUtil.equals("2", navAfterSellOrder.getType()) && StrUtil.equals("1", navSellOrderService.warehouseHandler(navAfterSellOrder.getDeliveryWarehouse()))) {//1:仅退款 2:补发     //1 - 本地仓; 2 - 海外仓; 3 - FBA
//                    // 还库存
//                    this.addInventory(navAfterSellOrder.getId());
//                }
//            }
            //03 更新状态 1:待提交 2:待审核 3:处理中 4:已完成 -1:已作废
            this.changeNavAfterSellOrderStatus(list, "-1", null);
        } catch (Exception ex) {
            log.error("发生异常,异常信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
            throw new RuntimeException("发生异常,信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
        }
    }


    /**
     * 客服备注 :用户在系统中为订单添加的备注信息，添加/编辑后，仓库在对应订单中可查看到该备注，用于订单特殊信息传递给仓库人员，或做订单备注记录；
     * <p>
     * 修改订单客服备注，备注订单用什么卡片，让仓库配货
     */
    @DataActivity(
            action = SysDataActivityAction.Other,
            message = "%s执行了客服备注操作"
    )
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void customRecordRemark(CustomNavAfterSellOrderRecordRemarkActionVo vo) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(vo.getIdList()), "请选择订单!");
        Assert.isTrue(StrUtil.isNotBlank(vo.getRemark()), "备注为空!");
        if (ObjUtil.isEmpty(vo.getReplace())) {
            vo.setReplace(false);
        }
        try {
            List<NavAfterSellOrder> list = this.listByIds(vo.getIdList());
            for (NavAfterSellOrder item : list) {
                JwtUserInfo currentUser = JwtUserInfo.fromHeader();
                if (!vo.getReplace()) {
                    item.setRemark(StrUtil.concat(true, item.getRemark(), "  ", vo.getRemark()));
                } else {
                    item.setRemark(StrUtil.concat(true, vo.getRemark()));
                }
                item.setLastUpdateTime(new Date());
                if (currentUser != null) {
                    item.setLastUpdateUser(currentUser.getUserId());
                }
            }
            this.updateBatchById(list);
        } catch (Exception ex) {
            log.error("发生异常,异常信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
            throw new RuntimeException("发生异常,信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
        }
    }

    /**************************************公用方法******************************************/
    /**
     * 更新订单管理状态
     *
     * @param list
     * @param status 待发货的订单 1:待审核 2:待发货 3:发货中 4:已发货 5:不发货 6:平台发货 -1:已取消
     */
    @Transactional(rollbackFor = Exception.class)
    public void changeNavAfterSellOrderStatus(List<NavAfterSellOrderVo> list, String status, String refundStatus) {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
        for (NavAfterSellOrder entity : list) {
            // 获取简单状态
            String rawSimpleStatus = entity.getStatus();
            entity.setStatus(status);
            if (StrUtil.isNotBlank(refundStatus)) {
                entity.setRefundStatus(refundStatus);
            }
            entity.setLastUpdateTime(new Date());
            //如果存在当前用户
            if (currentUser != null) {
                entity.setLastUpdateUser(currentUser.getUserId());
            }
            // 简单流程状态更新
            if (status != null) {
                simpleStatusService.afterUpdate(
                        "NavAfterSellOrder",
                        currentUser == null ? "" : currentUser.getDisplayName(),
                        entity.getId(),
                        rawSimpleStatus,
                        status
                );
            }
        }
        this.updateBatchById(BeanUtil.copyToList(list, NavAfterSellOrder.class));
    }
}
